﻿namespace DistrRTgen
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Diagnostics;

    public enum LoggerLevel
    {
        LOG_DEBUG,
        LOG_INFO,
        LOG_ALERT,
        LOG_ERROR
    }

    public class Logger : MarshalByRefObject
    {
        private static DistrRTgenConnector _Connection;
        private static ArrayList _Log = new ArrayList();

        public static void AddLogLine(string Line, LoggerLevel Level)
        {
            if (_Log.Count > 0x1388)
            {
                _Log.RemoveAt(0);
            }
            LoggedEvent evt = new LoggedEvent(Line, Level);
            _Log.Add(evt);
            string source = "DistrRTgen Service";
            string log = "Application";
            if (!EventLog.SourceExists(source))
            {
                EventLog.CreateEventSource(source, log);
            }
            EventLog eLog = new EventLog();
            eLog.Source = source;
            if (Level == LoggerLevel.LOG_ERROR)
            {
                if (_Connection == null)
                {
                    _Connection = new DistrRTgenConnector();
                }
                _Connection.ReportError(Line);
            }
            try
            {
                if (Level > Settings.EventLogThreshold)
                {
                    switch (Level)
                    {
                        case LoggerLevel.LOG_DEBUG:
                        case LoggerLevel.LOG_INFO:
                            eLog.WriteEntry(Line, EventLogEntryType.Information);
                            return;

                        case LoggerLevel.LOG_ALERT:
                            eLog.WriteEntry(Line, EventLogEntryType.Warning);
                            return;

                        case LoggerLevel.LOG_ERROR:
                            eLog.WriteEntry(Line, EventLogEntryType.Error);
                            return;
                    }
                }
            }
            catch (Win32Exception)
            {
            }
        }

        public LoggedEvent[] GetLastEntries(LoggerLevel filter)
        {
            return this.GetLastEntries(filter, 0);
        }

        public LoggedEvent[] GetLastEntries(LoggerLevel filter, uint MinEventID)
        {
            LoggedEvent[] evt;
            if (_Log.Count > 100)
            {
                evt = new LoggedEvent[100];
            }
            else
            {
                evt = new LoggedEvent[_Log.Count];
            }
            int i = _Log.Count - 1;
            int numFound = 0;
            while ((numFound < 100) && (i > 0))
            {
                LoggedEvent myEvent = (LoggedEvent) _Log[i];
                if (MinEventID > myEvent.EventID)
                {
                    return evt;
                }
                if (myEvent.Level >= filter)
                {
                    evt[numFound++] = (LoggedEvent) _Log[i];
                }
                i--;
            }
            return evt;
        }

        public static string GetLevelString(LoggerLevel Level)
        {
            switch (Level)
            {
                case LoggerLevel.LOG_DEBUG:
                    return "Debug";

                case LoggerLevel.LOG_INFO:
                    return "Info";

                case LoggerLevel.LOG_ALERT:
                    return "Alert";

                case LoggerLevel.LOG_ERROR:
                    return "Error";
            }
            return "Unknown level";
        }
    }
}

